package myarraylist;

import java.util.Arrays;

public class MyArrayList implements IList{
    int MyArrayList(){
        return 20;
    }
    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_SIZE=10;
    public MyArrayList(){
        this.elem=new int[DEFAULT_SIZE];
    }
    public MyArrayList(int custom){
        this.elem=new int[custom];
    }

    public boolean isFull(){
        return usedSize==elem.length;
    }
    public void Expansion(){
        if (isFull()){
            elem= Arrays.copyOf(elem,elem.length*2);
        }
    }
    //1.首先判断数组是否已经满了,满了就要进行扩容
    //2.将data存放到elem中
    //3.usedSize++;
    @Override
    public void add(int data) {
        //1.首先判断数组是否已经满了,满了就要进行扩容
        Expansion();
        //2.将data存放到elem中
        elem[usedSize]=data;
        //3.usedSize++;
        usedSize++;
    }


    public void isLegal(int pos)throws PosLegal{
        if (pos<0||pos>usedSize){
            throw new PosLegal("pos不合法");
        }
    }
    //1.判断pos的合法性
    @Override
    public void add(int pos, int data) {
        try{
            isLegal(pos);
        }catch (PosLegal e){
            e.printStackTrace();
        }
        Expansion();

        for (int i = usedSize-1; i >=pos; i--) {
            elem[i+1]=elem[i];
        }
        elem[pos]=data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind){
        if (size()==0){
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            if (elem[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if (size()==0){
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            if (elem[i]==toFind){
                return i;
            }
        }
        return -1;
    }


    public void isLegalGetandSet(int pos)throws PosLegal{
        if (pos<0||pos>usedSize){
            throw new PosLegal("pos不合");
        }
    }

    public boolean isEmpty(){
        return usedSize==0;
    }
    @Override
    public int get(int pos){
        isLegalGetandSet(pos);
        if (isEmpty()){
            throw new ElemEmpty("顺序表为空");
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        isLegalGetandSet(pos);
        elem[pos]=value;
    }

    @Override
    public void remove(int toRemove) {
        if (isEmpty()){
            return;
        }
        int index=indexOf(toRemove);
        if (index==-1){
            System.out.println("找不到该数字");
        }
        for (int i = index; i < usedSize-1; i++) {
            elem[i]=elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }
}
